package com.hds.modules.app.entity.wx.po;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.hds.modules.app.entity.wx.common.WxConstants;
import com.hds.modules.app.entity.wx.utils.WxPayUtils;

/**
 * 关闭订单接口返回结果数据类
 * @version 1.0
 * 微信官方api连接地址：https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3
 */
public class CloseOrderResult {

    @Override
    public String toString() {
        return "CloseOrderResult [" + (return_code != null ? "return_code=" + return_code + ", " : "")
                + (return_msg != null ? "return_msg=" + return_msg + ", " : "")
                + (appid != null ? "appid=" + appid + ", " : "") + (mch_id != null ? "mch_id=" + mch_id + ", " : "")
                + (nonce_str != null ? "nonce_str=" + nonce_str + ", " : "")
                + (sign != null ? "sign=" + sign + ", " : "")
                + (result_code != null ? "result_code=" + result_code + ", " : "")
                + (result_msg != null ? "result_msg=" + result_msg : "") + "]";
    }

    /**
     * 返回状态码
     */
    private String return_code;

    /**
     * 返回信息
     */
    private String return_msg;

    /**
     * 公众账号ID
     */
    private String appid;

    /**
     * 商户号
     */
    private String mch_id;

    /**
     * 随机字符串
     */
    private String nonce_str;

    /**
     * 签名
     */
    private String sign;

    /**
     * 业务结果
     */
    private String result_code;

    /**
     * 业务结果描述
     */
    private String result_msg;

    /**
     * 【非必填】
     * 错误代码
     */
    private String err_code = "";

    /**
     * 【非必填】
     * 错误代码描述
     */
    private String err_code_des = "";

    public String getReturn_code() {
        return return_code;
    }

    public void setReturn_code(String return_code) {
        this.return_code = return_code;
    }

    public String getReturn_msg() {
        return return_msg;
    }

    public void setReturn_msg(String return_msg) {
        this.return_msg = return_msg;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getResult_code() {
        return result_code;
    }

    public void setResult_code(String result_code) {
        this.result_code = result_code;
    }

    public String getResult_msg() {
        return result_msg;
    }

    public void setResult_msg(String result_msg) {
        this.result_msg = result_msg;
    }

    public String getErr_code() {
        return err_code;
    }

    public void setErr_code(String err_code) {
        this.err_code = err_code;
    }

    public String getErr_code_des() {
        return err_code_des;
    }

    public void setErr_code_des(String err_code_des) {
        this.err_code_des = err_code_des;
    }

    /**
     * 解析微信关闭订单接口执行后的返回数据
     * @param resultXML
     * @return
     */
    public CloseOrderResult getCloseOrderResult(String resultXML,String apiKey)
    {
        List<String> paramNameList = new ArrayList<String>();
        Map<String,String> paramValueMap = new HashMap<String,String>();
        try {
            //构建xml文档对象
            Document doc = DocumentHelper.parseText(resultXML);

            //获取xml根元素
            Element root = doc.getRootElement();
            //获取return_code节点
            Element return_codeE = root.element("return_code");
            //获取return_msg节点
            Element return_msgE = root.element("return_msg");
            String return_code = return_codeE==null?WxConstants.FAIL:return_codeE.getTextTrim();
            String return_msg = return_msgE==null?"":return_msgE.getTextTrim();

            this.setReturn_code(return_code);
            this.setReturn_msg(return_msg);

            //如果return_code 为 SUCCESS
            if(WxConstants.SUCCESS.equals(return_code))
            {
                Element appidE = root.element("appid");
                Element mch_idE = root.element("mch_id");
                Element nonce_strE = root.element("nonce_str");
                Element signE = root.element("sign");
                Element result_codeE = root.element("result_code");
                Element result_msgE = root.element("result_msg");
                Element err_codeE = root.element("err_code");
                Element err_code_desE = root.element("err_code_des");

                String appid = appidE==null?"":appidE.getTextTrim();
                String mch_id = mch_idE==null?"":mch_idE.getTextTrim();
                String nonce_str = nonce_strE==null?"":nonce_strE.getTextTrim();
                String sign = signE==null?"":signE.getTextTrim();
                String result_code = result_codeE==null?"":result_codeE.getTextTrim();
                String result_msg = result_msgE==null?"":result_msgE.getTextTrim();
                String err_code = err_codeE==null?"":err_codeE.getTextTrim();
                String err_code_des = err_code_desE==null?"":err_code_desE.getTextTrim();

                this.setAppid(appid);
                this.setMch_id(mch_id);
                this.setNonce_str(nonce_str);
                this.setSign(sign);
                this.setResult_code(result_code);
                this.setResult_msg(result_msg);
                this.setErr_code(err_code);
                this.setErr_code_des(err_code_des);

                //如果 result_code 为SUCCESS
                if(WxConstants.SUCCESS.equals(result_code))
                {
                    /*由于微信文档说明可能会随时对返回数据增加字段,所以为了能正确验签要动态获取参加验签的数据*/
                    WxPayUtils.getSignData(root, paramNameList, paramValueMap);

                    String[] paramNames = new String[paramNameList.toArray().length];
                    paramNameList.toArray(paramNames);
                    String checkSign = WxPayUtils.getPaySignMD5(paramNames, paramValueMap);

                    if(!checkSign.equals(this.getSign()))
                    {
                        this.setResult_code(WxConstants.FAIL);
                        this.setErr_code("SIGNERROR");
                        this.setErr_code_des("关闭订单返回数据验签失败");
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            this.setReturn_code(WxConstants.FAIL);
            this.setReturn_msg("统一下单返回数据解析失败");
        }
        return this;
    }



}